package com.gsbelarus.gedemin.salary.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.ksoichiro.android.observablescrollview.ObservableRecyclerView;
import com.github.ksoichiro.android.observablescrollview.ObservableScrollViewCallbacks;
import com.github.ksoichiro.android.observablescrollview.ScrollState;
import com.gsbelarus.gedemin.lib.ui.fragment.FragmentModel;
import com.gsbelarus.gedemin.salary.R;
import com.gsbelarus.gedemin.salary.adapter.DrawerPersonInfoAdapter;
import com.gsbelarus.gedemin.salary.database.RealmHelper;
import com.gsbelarus.gedemin.salary.entity.DrawerPersonInfoItem;
import com.gsbelarus.gedemin.salary.entity.model.PayslipModel;
import com.gsbelarus.gedemin.salary.ui.CircularImageView;
import com.gsbelarus.gedemin.salary.ui.DrawerPersonInfoItemDecoration;
import com.gsbelarus.gedemin.salary.util.CurrencyHelper;
import com.nineoldandroids.view.ViewHelper;

import java.text.DecimalFormat;

import io.realm.Realm;

public class DrawerPersonInfoFragment extends FragmentModel {

    public static final String SAVE_RESOURCE_DRAWABLE = "resource_drawable";
    public static final String SAVE_RESOURCE_DRAWABLE_ACCOUNT = "resource_drawable_account";

    private DrawerPersonInfoAdapter adapter;
    private TextView firstSurNameView;
    private RealmHelper realmHelper;

    @Override
    public int getViewResource() {
        return R.layout.drawer_person_info;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        realmHelper = new RealmHelper(Realm.getInstance(context));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        realmHelper.getRealm().close();
    }

    @Override
    public void onCreateViewFragment(View rootView) {
        super.onCreateViewFragment(rootView);

        firstSurNameView = (TextView) rootView.findViewById(R.id.first_sur_name);
        final FrameLayout accountLayout = (FrameLayout) rootView.findViewById(R.id.account_layout);
        final FrameLayout accountViewLayout = (FrameLayout) rootView.findViewById(R.id.account_view_layout);
        final ImageView backgroundImageView = (ImageView) rootView.findViewById(R.id.drawer_person_info_wallpapers);
        final CircularImageView accountImageView = (CircularImageView) rootView.findViewById(R.id.drawer_person_info_account);
        int resourceDrawableAccount = getArguments().getInt(SAVE_RESOURCE_DRAWABLE_ACCOUNT, 0);
        int resourceDrawable = getArguments().getInt(SAVE_RESOURCE_DRAWABLE, 0);
        if (resourceDrawable != 0 && resourceDrawableAccount != 0) {
            backgroundImageView.setImageResource(resourceDrawable);
            accountImageView.setImageResource(resourceDrawableAccount);
        }

        ObservableRecyclerView recyclerView = (ObservableRecyclerView) rootView.findViewById(R.id.drawer_person_info_recycler_view);
        if (recyclerView == null) {
            recyclerView = (ObservableRecyclerView) rootView.findViewById(R.id.drawer_person_info_recycler_view_land);
        } else {
            recyclerView.setScrollViewCallbacks(new ObservableScrollViewCallbacks() {

                @Override
                public void onScrollChanged(int i, boolean b, boolean b2) {
                    ViewHelper.setTranslationY(backgroundImageView, (-i - backgroundImageView.getMeasuredHeight() -
                            accountViewLayout.getMeasuredHeight()) / 2);
                    ViewHelper.setTranslationY(accountLayout, -i - accountLayout.getMeasuredHeight());

                }

                @Override
                public void onDownMotionEvent() {
                }

                @Override
                public void onUpOrCancelMotionEvent(ScrollState scrollState) {
                }
            });

        }
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        adapter = new DrawerPersonInfoAdapter(context);
        recyclerView.setHasFixedSize(false);
        recyclerView.addItemDecoration(new DrawerPersonInfoItemDecoration(context));
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void readDataBase() {

    }

    @Override
    public void initView(View view) {
        PayslipModel lastPayslipModel = realmHelper.getLastPayslip();

        String name;
        String listNumber;
        String department;
        String position;
        String titleSum;
        String sum;

        if (lastPayslipModel == null) {
            name = getString(R.string.drawer_header_item_text);
            listNumber = "...";
            department = "...";
            position = "...";
            titleSum = getString(R.string.person_salary);
            sum = "...";

        } else {
            name = lastPayslipModel.getEmployee().getFirstname() + " " +
                    lastPayslipModel.getEmployee().getSurname();
            listNumber = lastPayslipModel.getEmployee().getListNumber();
            department = lastPayslipModel.getDepartment();
            position = lastPayslipModel.getPosition();

            DecimalFormat decimalFormat = (DecimalFormat) CurrencyHelper.getInstance(context).getFormatter().clone();
            decimalFormat.applyPattern(CurrencyHelper.FORMAT_LONG_SUM);
            if (lastPayslipModel.getSalary() == 0) {
                titleSum = getString(R.string.person_hour_rate);
                sum = decimalFormat.format(lastPayslipModel.getHourRate()) + " руб.";
            } else {
                titleSum = getString(R.string.person_salary);
                sum = decimalFormat.format(lastPayslipModel.getSalary()) + " руб.";
            }
        }

        adapter.clear();
        adapter
                .add(new DrawerPersonInfoItem(getString(R.string.person_department),
                        department, R.mipmap.ic_map_marker_radius_grey600_36dp))
                .add(new DrawerPersonInfoItem(getString(R.string.person_position),
                        position, R.mipmap.ic_readability_grey600_36dp))
                .add(new DrawerPersonInfoItem(titleSum,
                        sum, R.mipmap.ic_coin_grey600_36dp))
                .add(new DrawerPersonInfoItem(getString(R.string.person_number),
                        listNumber, R.mipmap.ic_account_key_grey600_36dp));
        firstSurNameView.setText(name);
    }
}
